package com.hangsu.train.components.web.controller;

import com.hangsu.train.components.entity.Org;
import com.hangsu.train.components.params.IdParam;
import com.hangsu.train.components.params.OrgAddParam;
import com.hangsu.train.components.service.OrgService;
import com.hangsu.train.core.result.Resp;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/org")
public class OrgController {

    private OrgService orgService;

    @Resource
    public void setOrgService(OrgService orgService) {
        this.orgService = orgService;
    }

    @PostMapping("/list")
    public Resp<List<Org>> list() {
        List<Org> tree = orgService.tree();
        return Resp.ok(tree);
    }

    @PostMapping("/create")
    public Resp<?> create(@RequestBody @Valid OrgAddParam param) {
        orgService.create(param);
        return Resp.ok();
    }

    @PostMapping("/del")
    public Resp<?> del(@RequestBody @Valid IdParam param) {
        orgService.del(param.getId());
        return Resp.ok();
    }

}
